/**
 * Created by sam on 16-11-21.
 */

var cheerio = require('cheerio');
var dao = require('./dao');
var utils = require('./utils');
var cookie = require('./cookie');
var common = require('../conf/common');

function loadComment(asin,ep,num,page,commTime) {
    var source = common.getSource();
    var url = common.getReviewUrl(asin,page);
    utils.getc(url,num,function (html) {
        if(html === 'timeout'){
            ep.emit("sleep", asin);
        }else{
            var $ = cheerio.load(html);
            if($("#captchacharacters").length>0){
                console.log("Thread["+num+"] .... robot ....");
                ep.emit("sleep", asin);
            }else{

                //Most recent 评论
                var yesterday = new Date(commTime);
                var flag = true;
                var params = [];
                //var tmp = "";
                $("#cm_cr-review_list>div").each(function (index) {
                    var reviewId = $(this).attr("id");
                    if(reviewId) {
                        var comm_url = $("a.review-title",$(this)).attr('href');
                        var comm_grade = $("div:nth-child(1) > a:nth-child(1) > i > span",$(this)).text().replace(" out of 5 stars","");
                        var comm_title = $("a.review-title",$(this)).text();
                        var comm_name = $("a.author",$(this)).text();
                        var cust_url = $("a.author",$(this)).attr('href');
                        var comm_date = $("span.review-date",$(this)).text();
                        var comm_color = $("div.a-row.a-spacing-mini.review-data.review-format-strip > a",$(this)).text();
                        var comment = $("span.review-text",$(this)).text().trim();
                        var comm_imgs = '';
                        $(".review-image-tile",$(this)).each(function () {
                            comm_imgs += $(this).attr("src")+";";
                        });

                        var comm_create = common.formatDate(comm_date);
                        var cd = comm_create.split("-");
                        var cDay = new Date(cd[0],parseInt(cd[1])-1,parseInt(cd[2]));
                        flag = yesterday > cDay;
                        //tmp = cDay;
                        /*console.log("================================");
                        console.log("reviewId:"+reviewId+",createDate:"+comm_create+",grade:"+comm_grade+",title:"+comm_title);
                        console.log("name:"+comm_name+",date:"+comm_date+",color:"+comm_color+",cust_url:"+cust_url);
                        console.log("comm_url:"+comm_url);
                        console.log("comm_cent:"+comment);
                        console.log("comm_imgs:"+comm_imgs);
                        console.log("================================");
                        console.log("");*/

                        var data = {
                            fetchTime:common.getFetchTime(),
                            page:page,
                            page_set:index,
                            source:source,
                            reviewId:reviewId,
                            comm_url:common.getDomain()+comm_url,
                            comment:utils.replaceEmoji(comment),
                            comm_imgs:comm_imgs,
                            comm_grade:comm_grade,
                            comm_title:utils.replaceEmoji(comm_title),
                            comm_name:comm_name,
                            cust_url:common.getDomain()+cust_url,
                            comm_date:comm_date,
                            comm_create:comm_create,
                            comm_color:comm_color.replace("Color: ",""),
                            asin:asin,
                        };

                        //console.log(dd);

                        //dao.saveProCMCR(dd);
                       // dao.saveHotkeyCMCR(dd);
                        params.push([data.fetchTime,data.comm_create,data.asin, data.comm_name, data.comm_color, data.cust_url, data.comm_grade, data.comment,
                            data.comm_title, data.comm_url, data.comm_date, data.asin, data.reviewId,data.page,data.page_set]);
                    }
                });

                var sql = "replace into t_az"+source+"_hot_key_comment(fetch_time,comment_create_date,asin,comment_name,pro_color,cust_url,comment_grade" +
                    ",comment_content,title,comment_url,original_date,url_asin,review_id,comment_page,comment_page_set) values(?,?,?,?,?,?,?, ?,?,?,?,?,?,?,?)";

                dao.execTrans(sql,params,function (err) {
                    if(err) console.log(err);

                    var len =$("#cm_cr-review_list>div").length;
                    //if(flag || len < 10){//当前页10条评论，都是两天内的就翻页，否则就next asin
                    if(len < 10||flag||page>99){//当前页10条评论，都是两天内的就翻页，否则就next asin
                        ep.emit('next_asin', asin);

                    }else{
                        ep.emit('next_page', asin);
                    }
                });
            }
        }
    })
}

module.exports.loadComment = loadComment;



